package gnss

import (
	"bytes"
	"fmt"
	"io"
	"math"
)

var (
	RINEX_TransVer     string = "D10.RINEX.Ver 1.02"
	RINEX_CopyRight    string = "D10.RINEX"
	Warning_Obs_Exists bool   = true
)

/*
static void outrnxobsf(FILE *fp, double obs, int lli)
{
    if (obs==0.0||obs<=-1E9||obs>=1E9) {
        fprintf(fp,"              ");
    }
    else {
        fprintf(fp,"%14.3f",obs);
    }
    if (lli<0||!(lli&(LLI_SLIP|LLI_HALFC|LLI_BOCTRK))) {
        fprintf(fp,"  ");
    }
    else {
        fprintf(fp,"%1.1d ",lli&(LLI_SLIP|LLI_HALFC|LLI_BOCTRK));
    }
}
*/

func OutRnxObsf(w io.Writer, obs float64, lli int) {
	if obs == 0 || obs < -1e9 || obs >= 1e9 {
		w.Write([]byte("              "))
	} else {
		fmt.Fprintf(w, "%14.3f", obs)
	}
}

/***
 * 按照rinex格式将浮点数格式化成字符串, 可用于写观测值
 *    前面不足补充空格, 如果为0直接输出指定宽度的字符串
 */
func PrintRinexFV(fv float64, digitN int, width int) string {
	str := ""
	if math.IsInf(fv, 0) {
		fv = 0
	}
	if fv == 0 {
		str += string(bytes.Repeat([]byte(" "), width))
		return str
	}
	v := fmt.Sprintf("%."+fmt.Sprintf("%d", digitN)+"f", fv)
	l := width - len(v)
	if l > 0 {
		str += string(bytes.Repeat([]byte(" "), l))
	}
	str += v
	return str
}

/***
 * 按照rinex格式将浮点数格式化成字符串
 *    前面不足补充空格
 */
func FPrintRinexEV(fv float64, digitN int) string {
	str := ""
	v := fmt.Sprintf("%."+fmt.Sprintf("%d", digitN)+"E", fv)
	l := 12 - len(v)
	if l > 0 {
		str += string(bytes.Repeat([]byte(" "), l))
	}
	str += v

	return str
}

/***
 * 按照rinex格式将浮点数格式化成字符串
 *    前面不足补充空格
 */
func FPrintRinexStrV(v string, width int) string {
	l := width - len(v)
	if l > 0 {
		v = string(bytes.Repeat([]byte(" "), l)) + v
	}
	return v
}
